/*
* Copyright (c) 2015â2017 Zerone.Co.Ltd. All rights reserved. Inc.
* Copyright (c) 2017 All Rights Reserved.
* create by glassescat
* date:2017-09-11
*/
package com.zerone.pay.trade.web.controller;

import com.acooly.core.common.web.AbstractJQueryEntityController;
import com.acooly.module.security.utils.ShiroUtils;
import com.zerone.pay.merchant.repository.entity.MerchantEntity;
import com.zerone.pay.merchant.repository.service.MerchantRepositoryService;
import com.zerone.pay.trade.enums.TradeStatusEnum;
import com.zerone.pay.trade.repository.entity.Trade;
import com.zerone.pay.trade.repository.service.TradeRepositoryService;
import com.zerone.payment.common.exception.PaymentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * trade 管理控制器
 * 
 * @author glassescat
 * Date: 2017-09-11 00:05:08
 */
@Controller
@RequestMapping(value = "/manage/trade/trade")
public class TradeManagerController extends AbstractJQueryEntityController<Trade, TradeRepositoryService> {

	private static final String merchantTag = "merchantTag";
	

	{
		allowMapping = "*";
	}

	@SuppressWarnings("unused")
	@Autowired
	private TradeRepositoryService tradeRepositoryService;

	@Override
	protected void referenceData(HttpServletRequest request, Map<String, Object> model) {
		model.put("tradeStatusList", TradeStatusEnum.mapping());
	}

	@RequestMapping({"agentIndex"})
	public String agentIndex(HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute(merchantTag,"agentTrade");
		model.addAttribute("agentMerchant",findTradeByEmailOrMobile());
		return super.index(request,response,model);
	}

	@RequestMapping({"normalIndex"})
	public String normalIndex(HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute(merchantTag,"normalTrade");
		model.addAttribute("normalMerchant",findTradeByEmailOrMobile());
		return super.index(request,response,model);
	}

	private MerchantEntity findTradeByEmailOrMobile() {
		String merchant = ShiroUtils.getCurrentUser().getUsername();
		MerchantEntity entity = merchantRepositoryService.findByEmailOrMobile(merchant, merchant);
		if (entity == null) {
			throw new PaymentException("商户不存在");
		}
		return entity;
	}

	@Autowired
	private MerchantRepositoryService merchantRepositoryService;
}
